package com.wuming.house.service.impl;


import com.wuming.house.core.*;
import com.wuming.house.dao.MemberMapper;
import com.wuming.house.model.Member;
import com.wuming.house.service.MemberService;
import com.wuming.house.util.DateUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/***
 * Created by hanfei on 16/5/7.
 */
@Service("memberService")
public class MemberServiceImpl extends BaseServiceImpl implements MemberService {

    private static final Long EXPIRING = 1296000000L;

    @Autowired
    private MemberMapper memberMapper;

    @Override
    public void saveMember(Member member) throws BusinessException {
        memberMapper.insertSelective(member);
    }

    @Override
    public void modifyMember(Member member) throws BusinessException {
        memberMapper.updateByPrimaryKeySelective(member);
    }

    @Override
    public Member getMemberById(Integer id) throws BusinessException {
        return memberMapper.selectByPrimaryKey(id);
    }

    @Override
    public void removeMemberById(Integer id) throws BusinessException {
        memberMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void verifyMemberById(Integer id) throws BusinessException {
        Member member = new Member();
        member.setId(id);
        member.setStatus(MemberStatus.STATUS_NORMAL.ordinal());
        memberMapper.updateByPrimaryKeySelective(member);
    }

    @Override
    public Member getMemberByNameAndPassword(String userName, String password) {
        if (StringUtils.isBlank(userName)) {
            throw new BusinessException(WumingErrorCode.HUIQIAN_USERCENTER_DATABASE_OPERATE_ERROR);
        }
        if (StringUtils.isBlank(password)) {
            throw new BusinessException(WumingErrorCode.HUIQIAN_USERCENTER_PASSWORD_NULL);
        }
        Member member = new Member();
        member.setUserName(userName);
        member.setPassword(password);
        return memberMapper.selectByNameAndPassword(member);
    }

    @Override
    public boolean isAdministrator(Member user) {
        if (WumingRole.WUMING_USERCENTER_ROLE_ADMINISTRATOR.ordinal() == user.getRole().intValue()) {
            return true;
        }
        return false;
    }

    @Override
    public List<Member> getMembersByPage(int pageIndex, int pageSize, Integer memberId, String userName, String company, String phone, Integer status) throws BusinessException {

        try {
            Map<Object, Object> maps = new HashMap<Object, Object>();
            if (null != memberId) {
                maps.put("memberId", memberId);
            }
            maps.put("userName", userName);
            maps.put("company", company);
            maps.put("phone", phone);
            maps.put("status", status);
            return (List<Member>) this.getPageList(MemberMapper.class, "selectMembers", maps, pageIndex, pageSize);
        } catch (Exception e) {
            logger.error("查询用户信息失败{}" + e.getMessage());
            throw new BusinessException(WumingErrorCode.HUIQIAN_USERCENTER_DATABASE_OPERATE_ERROR);
        }
    }

    @Override
    public int getTotal(Integer memberId, String userName, String company, String phone, Integer status) throws BusinessException {
        try {
            Map<Object, Object> maps = new HashMap<Object, Object>();
            if (null != memberId) {
                maps.put("memberId", memberId);
            }
            maps.put("userName", userName);
            maps.put("company", company);
            maps.put("phone", phone);
            maps.put("status", status);
            return this.getTotal(MemberMapper.class, "selectTotal", maps);
        } catch (Exception e) {
            logger.error("查询用户信息总数失败.异常:{}", e.getMessage());
            throw new BusinessException(WumingErrorCode.HUIQIAN_USERCENTER_DATABASE_OPERATE_ERROR);
        }
    }


    @Override
    public Member getMemberByPhone(String phone) throws BusinessException {
        Member member = new Member();
        member.setPhone(phone);
        try {
            List<Member> members = memberMapper.selectMembers(member);
            if (null != member && !members.isEmpty()) {
                return members.get(0);
            }
            return null;
        } catch (Exception e) {
            logger.error("根据手机号查询会员失败.异常：{}", e.getMessage());
            throw new BusinessException(WumingErrorCode.HUIQIAN_USERCENTER_DATABASE_OPERATE_ERROR);
        }
    }


}
